/*
 * @Author: greyqiu greyqiu@163.com
 * @Date: 2023-06-27 16:56:34
 * @LastEditors: greyqiu greyqiu@163.com
 * @LastEditTime: 2023-06-29 17:03:03
 * @FileName: easy_timer_config.h
 * @Description: 
 * 配置easy_timer所使用的时基单元信息，如时基单元的计数方向，计数器频率，获取计数器值的宏，时基初始化
 * 
 * Copyright (c) 2023 by ${git_name}, All Rights Reserved. 
 */

#ifndef _EASY_TIMER_CONFIG_H
#define _EASY_TIMER_CONFIG_H

#include "bsp.h"

#define SYSTICK_BASE              CPUTIMER0_BASE

#define COUNTER_DIR_UP          0
#define COUNTER_DIR_DOWN        1

#define GET_TIME_STAMP()        (CPUTimer_getTimerCount(SYSTICK_BASE)) // 获取当前计数器的值

#define TIMER_DERECTION         COUNTER_DIR_DOWN // ARM-CORTEX内核的systick都是向下计数

#define EASY_TIMER_CLOCK_FREQ   (DEVICE_SYSCLK_FREQ / 10) // 时基频率10Mhz

#define HW_TIMER_BIT            24 // ARM-CORTEX内核的systick定时器为24位
    
#if HW_TIMER_BIT == 16
#define MAX_TICKS 0XFFFF

#elif HW_TIMER_BIT == 24
#define MAX_TICKS 0XFFFFFF

#elif HW_TIMER_BIT == 32
#define MAX_TICKS 0XFFFFFFFF
#endif

static inline void systick_init(void) {

    CPUTimer_setPeriod(SYSTICK_BASE, MAX_TICKS);

    //
    // Set pre-scale counter to divide by 9 (SYSCLKOUT):
    //
    CPUTimer_setPreScaler(SYSTICK_BASE, 9);

    //
    // Initializes timer control register. The timer is stopped, reloaded,
    // free run disabled, and interrupt enabled.
    // Additionally, the free and soft bits are set
    //
    CPUTimer_stopTimer(SYSTICK_BASE);
    CPUTimer_reloadTimerCounter(SYSTICK_BASE);
    CPUTimer_setEmulationMode(SYSTICK_BASE,
                              CPUTIMER_EMULATIONMODE_STOPAFTERNEXTDECREMENT);

    CPUTimer_startTimer(CPUTIMER0_BASE);
}

#endif	/* _EASY_TIMER_CONFIG_H */

